package com.card.hwatt.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 登陆拦截
 * @author Administrator
 *
 */
public class SecurityInterceptor implements HandlerInterceptor {

	/**
	 * 考勤员主页
	 */
	private static final String LOGIN_URL = "/index.jsp";
	
	/**
	 * 维保单位主页
	 */
	private static final String USER_URL = "/userindex.jsp";

	/**
	 * 拦截处理
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		HttpSession session = request.getSession(true);
		String url = request.getRequestURI();
		if (url.contains("index")) {
			Object obj = session.getAttribute("logon");
			if (obj == null) {
				String path = request.getContextPath() + LOGIN_URL;
				response.sendRedirect(path);
				return false;
			}
		} else {
			Object obj = session.getAttribute("user");
			if (obj == null) {
				String path = request.getContextPath() + USER_URL;
				response.sendRedirect(path);
				return false;
			}
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest req, HttpServletResponse res,
			Object arg2, ModelAndView arg3) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest req,
			HttpServletResponse res, Object arg2, Exception arg3)
			throws Exception {
	}

}
